<?php

/**
 * ImagesModel.php 注释
 * 
 * @package 
 * @subpackage 
 * @author rendong01 rendong237@126.com
 * 
 * : tool.php 6 2012-07-28 15:01:50Z rendong237@gmail.com $
 */
class ImagesModel{
	/**
	 * 数据库对象
	 *
	 * @var Mysql
	 */
	private $db;
	
	/**
	 * 构造方法
	 *
	 * @param Mysql $db
	 */
	public function __construct($db)
	{
		$this->db = $db;
	}
	
	/**
	 * 添加
	 */
	public function add( array $value )
	{
		$v = array();
//		isset($value["id"]) && $v["id"] = (int)$value["id"];
		isset($value["name"]) && $v["name"] = $value["name"];
		isset($value["author"]) && $v["author"] = $value["author"];
		isset($value["image"]) && $v["image"] = $value["image"];
		isset($value["image2"]) && $v["image2"] = $value["image2"];
		isset($value["content"]) && $v["content"] = $value["content"];
		isset($value["seq"]) && $v["seq"] = (int)$value["seq"];
		isset($value["pubtime"]) && $v["pubtime"] = $value["pubtime"];
		
		$this->db->insert("images", $v);
	}
	
	/**
	 * 修改
	 */
	public function change($id, array $value)
	{
		$v = array();
		isset($value["name"]) && $v["name"] = $value["name"];
		isset($value["author"]) && $v["author"] = $value["author"];
		isset($value["image"]) && $v["image"] = $value["image"];
		isset($value["image2"]) && $v["image2"] = $value["image2"];
		isset($value["content"]) && $v["content"] = $value["content"];
		isset($value["seq"]) && $v["seq"] = (int)$value["seq"];
		isset($value["pubtime"]) && $v["pubtime"] = $value["pubtime"];

		$this->db->update("images", $v, 'id=' . (int)$id);
	}
	
	/**
	 * 删除
	 */
	public function del($id)
	{
		$this->db->delete("images", 'id=' .  (int)$id);
	}
	
	/**
	 * 读取列表
	 *
	 * @param Integer $nowp
	 * @param Integer $per_page
	 */
	public function fetchAll($nowp, $per_page)
	{
		$where = '';		
		$totalNum = $this->db->fetchOne ('select count(*) from images ' . $where);
        
        require_once APP . '/application/models/Util.php';
        
        $pageInfo = Util::page ($totalNum, $per_page, $nowp);
        $start    = $pageInfo ['start'];
        $list     = $this->db->fetchAll ('select * from images' . $where .' order by id desc limit ' . $start . ',' . $per_page);
        
        return Array ('page' => $pageInfo['page'], 'list' => $list);
	}
	
	/**
	 * 显示
	 */
	public function show($id)
	{
		return $this->db->fetchRow("select * from `images` where id=\"?\"", array($id));
	}
}